import {reactive} from "vue";

export default function () {
    const pagination = reactive({
        dataList: [],
        pageSize: 10,
        total: 0,
        currentPage: 1,
        condition: {},
        pagingFunction: null,
        afterPagingHandle: null,
        isInit: false,
    });
    const currentChangeHandle = (val)=> {
        pagination.currentPage = val
        ajaxPaging()
    }
    const ajaxPagingHandle = (val)=> {
        pagination.dataList = val.rows
        pagination.total = val.total
        pagination.isInit = true
        if (pagination.afterPagingHandle){
            pagination.afterPagingHandle()
        }
    }
    const ajaxPaging = ()=>{
        beforePagingHandle()
        if (pagination.pagingFunction) {
            pagination.pagingFunction(pagination.condition, ajaxPagingHandle)
        }
    }
    const beforePagingHandle = ()=> {
        let data = pagination.condition
        if (!data){
            data = {}
        }
        let pageDomain = {
            pageNum: pagination.currentPage,
            pageSize: pagination.pageSize
        }
        data.pageDomain = pageDomain
    }
    return {
        pagination,
        currentChangeHandle,
        ajaxPaging
    }
}
